//
//  xsEditeDigitalRobotTitleView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
// 编辑 样式 气泡 动画 层级
// 样式 气泡 动画 层级
// 层级 镜像 删除
// 更换形象  层级 镜像 删除

#import "xsEditeDigitalRobotTitleView.h"
#import "NSString+WordSize.h"
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

#define vHeight 55.0

@interface xsEditeDigitalRobotTitleView()<UIScrollViewDelegate>

@property (nonatomic,assign) xsEditeDigitalRobotType editeType;// 默认大标题
@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) UIButton *xsBackButton;

@property (nonatomic, strong) NSArray *bigTitleStrArr;
@property (nonatomic, strong) NSArray *smalltitleStrArr;
@property (nonatomic, strong) NSArray *bgImgtitleStrArr;//
@property (nonatomic, strong) NSArray *digitalRobotTitleStrArr;

@property (nonatomic, strong) NSArray *bigIconArr;
@property (nonatomic, strong) NSArray *smallIconArr;
@property (nonatomic, strong) NSArray *bgImgIconArr;//
@property (nonatomic, strong) NSArray *digitalRobotIconArr;


@property (nonatomic, strong) UIView *bigImgView;// 大标题
@property (nonatomic, strong) UIView *smallView;// 小标题
@property (nonatomic, strong) UIView *backgruondImgView;//背景图像
@property (nonatomic, strong) UIView *digitalRobotView;// 机器人图像


@end

@implementation xsEditeDigitalRobotTitleView



- (instancetype)initWithEditeType:(xsEditeDigitalRobotType)type{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
//        self.editeType = type;
        
        self.bigTitleStrArr = @[@"编辑",@"样式",@"层级"];//,@"气泡",@"动画"
        self.smalltitleStrArr = @[@"样式",@"气泡",@"动画",@"层级"];
        self.bgImgtitleStrArr = @[@"层级",@"镜像",@"删除"];
        self.digitalRobotTitleStrArr = @[@"更换形象",@"层级",@"镜像",@"删除"];
        
        
        self.bigIconArr = @[@"bossIPEditor",@"bossIPStyleImg",
                                 @"bossIPQiPao",@"bossIPDongHua",
                                 @"bossIPCengJi"];
        
        self.smallIconArr = @[@"bossIPStyleImg",@"ipboosHeadIcon",
                                   @"ipboosHeadIcon",@"bossIPCengJi"];
        
        self.bgImgIconArr = @[@"bossIPCengJi",@"bossIPJingXinag",
                                   @"bossIPDelete"];
        
        self.digitalRobotIconArr = @[@"bossIPChangeXingXinag",@"bossIPCengJi",
                                   @"bossIPJingXinag",@"bossIPDelete"];
        
        
        [self addSubview:self.bgScrollView];
//        [self.bgScrollView addSubview:self.xsContentView];
        
        [self addSubview:self.xsBackButton];
        
        
        [self addSubview:self.bigImgView];
        [self addSubview:self.smallView];
        [self addSubview:self.backgruondImgView];
        [self addSubview:self.digitalRobotView];
        
    }
    return self;
}



- (UIView *)bigImgView{
    if(!_bigImgView){
        _bigImgView = [[UIView alloc] init];
        _bigImgView.backgroundColor = UIColor.clearColor;
    }
    return _bigImgView;
}
- (UIView *)smallView{
    if(!_smallView){
        _smallView = [[UIView alloc] init];
        _smallView.backgroundColor = UIColor.clearColor;
    }
    return _smallView;
}
- (UIView *)backgruondImgView{
    if(!_backgruondImgView){
        _backgruondImgView = [[UIView alloc] init];
        _backgruondImgView.backgroundColor = UIColor.clearColor;
    }
    return _backgruondImgView;
}
- (UIView *)digitalRobotView{
    if(!_digitalRobotView){
        _digitalRobotView = [[UIView alloc] init];
        _digitalRobotView.backgroundColor = UIColor.clearColor;
    }
    return _digitalRobotView;
}

- (UIScrollView *)bgScrollView
{
    if(!_bgScrollView){
        _bgScrollView = [[UIScrollView alloc] init];
        _bgScrollView.backgroundColor = UIColor.clearColor;
        
        _bgScrollView.delegate = self;
        _bgScrollView.pagingEnabled = YES;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _bgScrollView;
}


- (void)buttonClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
    //xsEditeDigitalRobot_bigTitle,// 大标题                  --->对应    编辑 样式 气泡 动画 层级
    //xsEditeDigitalRobot_smallTitle,// 小标题                --->对应    样式 气泡 动画 层级
    //xsEditeDigitalRobot_backgroundImg, // 背景图像                  --->对应    层级 镜像 删除
    //xsEditeDigitalRobot_digitalRobotImg // 机器人图像        --->对应    更换形象  层级 镜像 删除

    
    if([self.delegate respondsToSelector:@selector(xsEditeDigitalRobotTitleViewItemButtonEventWithEditeType:withSelectIndex:)]){
        [self.delegate xsEditeDigitalRobotTitleViewItemButtonEventWithEditeType:self.editeType withSelectIndex:selectedIndex];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    
}


- (void)xsChangeFrame{
    self.bgScrollView.frame = CGRectMake(0, 0, self.bounds.size.width, vHeight);
    
    self.xsBackButton.frame = CGRectMake(0, 0, vHeight, vHeight);
    
    
//   bigImgView;// 大标题
//   smallView;// 小标题
//   backgruondImgView;//背景图像
//   digitalRobotView;// 机器人图像
    
    CGFloat bigImageViewH = vHeight;
    CGFloat bigImageViewX = CGRectGetMaxX(self.xsBackButton.frame);
    CGFloat bigImageViewW = self.width-bigImageViewX;
    CGFloat bigImageViewY = 0;
    
    self.bigImgView.frame = CGRectMake(bigImageViewX, bigImageViewY, bigImageViewW, bigImageViewH);
    self.smallView.frame = CGRectMake(bigImageViewX, bigImageViewY, bigImageViewW, bigImageViewH);
    self.backgruondImgView.frame = CGRectMake(bigImageViewX, bigImageViewY, bigImageViewW, bigImageViewH);
    self.digitalRobotView.frame =CGRectMake(bigImageViewX, bigImageViewY, bigImageViewW, bigImageViewH);
    
    
    // 大标题
    [self xsCreateButtonToViewWithEditeType:xsEditeDigitalRobot_bigTitle];
    // 小标题
    [self xsCreateButtonToViewWithEditeType:xsEditeDigitalRobot_smallTitle];
    // 背景图像
    [self xsCreateButtonToViewWithEditeType:xsEditeDigitalRobot_backgroundImg];
    // 机器人图像
    [self xsCreateButtonToViewWithEditeType:xsEditeDigitalRobot_digitalRobotImg];
  
//    // 设置内容视图的尺寸
//    self.xsContentView.frame = CGRectMake(0, 0,kScreenW, vHeight);
//    self.bgScrollView.contentSize = self.xsContentView.frame.size;
}

- (void)xsCreateButtonToViewWithEditeType:(xsEditeDigitalRobotType)edType{
    
    NSArray *tempTitleArr;
    NSArray *tempIconArr;
    
    
//   bigImgView;// 大标题
//   smallView;// 小标题
//   backgruondImgView;//背景图像
//   digitalRobotView;// 机器人图像
    UIView *tempBgView;
    if(edType==xsEditeDigitalRobot_bigTitle){// 大标题
       
        tempTitleArr = self.bigTitleStrArr;
        tempIconArr = self.bigIconArr;
        
        tempBgView = self.bigImgView;
    }
    else if(edType==xsEditeDigitalRobot_smallTitle){// 小标题
        
        tempTitleArr = self.smalltitleStrArr;
        tempIconArr = self.smallIconArr;
        tempBgView = self.smallView;
    }
    else if(edType==xsEditeDigitalRobot_backgroundImg){// 背景图像
        
        tempTitleArr = self.bgImgtitleStrArr;
        tempIconArr = self.bgImgIconArr;
        tempBgView = self.backgruondImgView;
    }
    else{// 机器人图像
        
        tempTitleArr = self.digitalRobotTitleStrArr;
        tempIconArr = self.digitalRobotIconArr;
        tempBgView = self.digitalRobotView;
    }
    
    
    UIFont *btnFont = [UIFont systemFontOfSize:16];
    
    CGFloat buttonH = vHeight;
    CGFloat xsBackImgW = vHeight;
    CGFloat leftMargin = 10.0;
    
    
  
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < tempTitleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *buttonTitleStr = tempTitleArr[i];
        
        CGFloat buttonW = buttonH;//[buttonTitleStr calculateWidth:btnFont withHeight:buttonH];
        if(edType==xsEditeDigitalRobot_digitalRobotImg){
            
            if(i==0){
                buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:buttonH];
                button.frame = CGRectMake(i * buttonW+leftMargin, 0, buttonW, buttonH);
            }
            else{
                button.frame = CGRectMake(CGRectGetMaxX(lastButton.frame), 0, buttonW, buttonH);
            }
        }
        else{
            button.frame = CGRectMake(i * buttonW+leftMargin, 0, buttonW, buttonH);
        }
        
        
//        if(lastButton){
//            button.frame = CGRectMake(i * buttonW, 0, buttonW, 50);
//        }
//        else{
//            button.frame = CGRectMake(i * buttonW, 0, buttonW, 50);
//        }
        button.backgroundColor =  UIColor.clearColor;//xrandomColor;//xrandomColor;
//        [button setTitle:self.buttonTitle[i] forState:UIControlStateNormal];
//        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.font = btnFont;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [tempBgView addSubview:button];
        
        
        CGFloat buttonImageViewWH = buttonW *0.5;
        if(edType==xsEditeDigitalRobot_digitalRobotImg){
            
            if(i==0){
                buttonImageViewWH = buttonH *0.5;
            }
        }
        CGFloat buttonImageViewX = (buttonW-buttonImageViewWH)*0.5;
        CGFloat buttonImageViewY = 3.0;
        
        
        NSString *buttonImageViewStr = tempIconArr[i];
        UIImage *inggg =  [UIImage imageNamed:buttonImageViewStr];
        UIImageView *buttonImageView = [[UIImageView alloc] init];
        buttonImageView.backgroundColor  =  UIColor.clearColor;//xrandomColor;//
        buttonImageView.userInteractionEnabled = NO;
        [button addSubview:buttonImageView];
        buttonImageView.frame = CGRectMake(buttonImageViewX, buttonImageViewY, buttonImageViewWH, buttonImageViewWH);
        buttonImageView.image = inggg;
        
        CGFloat buttonTitleLabelW = buttonW;
        CGFloat buttonTitleLabelX = 0;
        CGFloat buttonTitleLabelY = CGRectGetMaxY(buttonImageView.frame);//+3;
        CGFloat buttonTitleLabelH = buttonH-buttonTitleLabelY;//-3;
        UILabel *buttonTitleLabel = [[UILabel alloc] init];
        buttonTitleLabel.backgroundColor = UIColor.clearColor;//xrandomColor;//
        buttonTitleLabel.font = [UIFont systemFontOfSize:15];
        buttonTitleLabel.textColor = UIColor.whiteColor;
        buttonTitleLabel.numberOfLines = 1;
        buttonTitleLabel.text = buttonTitleStr;
        buttonTitleLabel.textAlignment = NSTextAlignmentCenter;
        [button addSubview:buttonTitleLabel];
        buttonTitleLabel.frame = CGRectMake(buttonTitleLabelX, buttonTitleLabelY, buttonTitleLabelW, buttonTitleLabelH);
        
        
        lastButton = button;
    }
    
    self.bgScrollView.contentSize = CGSizeMake(kScreenW, vHeight);
}

- (void)updateConstraints{
    [super updateConstraints];
    
   // __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");

}

- (void)showViewWithType:(xsEditeDigitalRobotType)type{
    
    self.editeType = type;
    
    NSLog(@"showViewWithType type: %lu", (unsigned long)type);
    
    //xsEditeDigitalRobot_bigTitle,// 大标题                  --->对应    编辑 样式 气泡 动画 层级
    //xsEditeDigitalRobot_smallTitle,// 小标题                --->对应    样式 气泡 动画 层级
    //xsEditeDigitalRobot_backgroundImg, // 背景图像                  --->对应    层级 镜像 删除
    //xsEditeDigitalRobot_digitalRobotImg // 机器人图像        --->对应    更换形象  层级 镜像 删除
    if(type==xsEditeDigitalRobot_bigTitle){
        
        self.bigImgView.hidden = NO;
        self.smallView.hidden = !self.bigImgView.hidden;
        self.backgruondImgView.hidden = !self.bigImgView.hidden;
        self.digitalRobotView.hidden = !self.bigImgView.hidden;
    }
    else if(type==xsEditeDigitalRobot_smallTitle){
       
        self.smallView.hidden = NO;
        self.bigImgView.hidden = !self.smallView.hidden;
        self.backgruondImgView.hidden = !self.smallView.hidden;
        self.digitalRobotView.hidden = !self.smallView.hidden;
    }
    else if(type==xsEditeDigitalRobot_backgroundImg){
        
        self.backgruondImgView.hidden = NO;
        self.smallView.hidden = !self.backgruondImgView.hidden;
        self.bigImgView.hidden = !self.backgruondImgView.hidden;
        self.digitalRobotView.hidden = !self.backgruondImgView.hidden;
    }
    else{//xsEditeDigitalRobot_digitalRobotImg
        
        self.digitalRobotView.hidden = NO;
        self.smallView.hidden = !self.digitalRobotView.hidden;
        self.backgruondImgView.hidden = !self.digitalRobotView.hidden;
        self.bigImgView.hidden = !self.digitalRobotView.hidden;
    }
}
- (UIButton *)xsBackButton{
    
    if(!_xsBackButton){
        _xsBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsBackButton.backgroundColor = UIColor.clearColor;
        [_xsBackButton setImage:[UIImage imageNamed:@"wqicon_nav_back_light"] forState:UIControlStateNormal];
        [_xsBackButton setImage:[UIImage imageNamed:@"wqicon_nav_back_light"] forState:UIControlStateSelected];
        [_xsBackButton setImageEdgeInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
        //        [_xsBackButton layoutButtonWithEdgeInsetsStyle:XSButtonEdgeInsetsStyleImageLeft imageTitleSpace:8.0];
        [_xsBackButton addTarget:self action:@selector(xsBackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _xsBackButton;
}

- (void)xsBackButtonClicked:(UIButton *)sender{
    
    
    NSLog(@"xsBackButtonClicked");
    
    if([self.delegate respondsToSelector:@selector(xsEditeDigitalRobotTitleViewBackButtonEvent:)]){
        [self.delegate xsEditeDigitalRobotTitleViewBackButtonEvent:sender];
    }
}

@end
